from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from rest_framework import generics, viewsets

from accessories.service import AccessoriesService
from django.core import serializers
from accessories.models import Accessories
from accessories.serializers import AccessoriesSerializer


def select_accessories(request):
    """
    搜索配件信息view
    :param request:
    :return: json
    """
    name = request.POST.get("name")
    return_accessories = AccessoriesService().select_accessories(name)
    if return_accessories is not None:
        result_json = serializers.serialize("json", return_accessories)
        result = {"code": 200, "status": "正确", "data": result_json}
        return HttpResponse(result)
    else:
        result = {"code": 200, "status": "错误", "data": None}
        result = serializers.serialize("json", result)
        return HttpResponse(result)


# class AccessoriesView(generics.ListAPIView):
#     """
#      查看接口列表
#     """
#     queryset = Accessories.accessories.all()
#     serializer_class = AccessoriesSerializer
class AccessoriesView(viewsets.ModelViewSet):
    """
     查看接口列表
    """
    queryset = Accessories.accessories.all()
    serializer_class = AccessoriesSerializer
